//
//  ShouldReceiveListViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/12/14.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class ShouldReceiveListViewController: RefreshedTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UINib.init(nibName: "ApprovalDetailCell", bundle: Bundle.main), forCellReuseIdentifier: "ApprovalDetailCell")
    }
    
    override func path(for name: String) -> String {
        return ShouldIncomeList
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "ApprovalDetailCell")!
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cel = cell as? ApprovalDetailCell {
            cel.accessoryType = .disclosureIndicator
            let item = self.source[indexPath.row]
            cel.titleLbl.text = item["contractName"].rawString()
            let amount = item["openingBalance"].int ?? 0
            let yearticket = item["invoiceMoney"].int ?? 0
            let back = item["backMoney"].int ?? 0
            let result = (amount + yearticket - back) < 0 ? 0 : (amount + yearticket - back) / 100
            cel.detailLbl.text = "期末余额:" + String(result).rmbformat + "元"
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let detail = ShouldReceiveDetailViewController()
        let item = source[indexPath.row]
        detail.title = "应收账款详情"
        detail.id = item["id"].rawString() ?? ""
        self.push(destination: detail)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
}
